const express = require('express');
const router = express.Router();
const userController = require('../controllers/userController');

/**
 * @swagger
 * tags:
 *   name: Users
 *   description: 用户相关的 API
 */

// 分页查询用户
/**
 * @swagger
 * /users/search:
 *   post:
 *     summary: 分页搜索用户
 *     tags: [Users]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               page:
 *                 type: integer
 *                 default: 1
 *               pageSize:
 *                 type: integer
 *                 default: 10
 *               username:
 *                 type: string
 *                 example: "john"
 *               email:
 *                 type: string
 *                 example: "user@example.com"
 *     responses:
 *       200:
 *         description: 搜索成功
 *         content:
 *           application/json:
 *             example:
 *               data:
 *                 - id: 1
 *                   username: "john_doe"
 *                   email: "john@example.com"
 *               total: 100
 *               page: 1
 *               pageSize: 10
 *       500:
 *         description: 服务器错误
 */
router.post('/search', userController.searchUsers);

// 获取全部用户信息
/**
 * @swagger
 * /users:
 *   get:
 *     summary: 获取所有用户列表
 *     tags: [Users]
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             example:
 *               - id: 1
 *                 username: "admin"
 *                 email: "admin@example.com"
 *               - id: 2
 *                 username: "user01"
 *                 email: "user@example.com"
 *       500:
 *         description: 服务器错误
 */
router.get('/', userController.getAllUsers);

// 获取单个用户信息
/**
 * @swagger
 * /users/{id}:
 *   get:
 *     summary: 获取单个用户详细信息
 *     tags: [Users]
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             example:
 *               id: 1
 *               username: "john_doe"
 *               email: "john@example.com"
 *               created_at: "2023-01-01T00:00:00Z"
 *       404:
 *         description: 用户不存在
 *       500:
 *         description: 服务器错误
 */
router.get('/:id', userController.getUserById);

// 创建用户
/**
 * @swagger
 * /users:
 *   post:
 *     summary: 创建新用户
 *     tags: [Users]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - username
 *               - email
 *               - password
 *             properties:
 *               username:
 *                 type: string
 *                 example: "new_user"
 *               email:
 *                 type: string
 *                 format: email
 *                 example: "new@example.com"
 *               password:
 *                 type: string
 *                 format: password
 *                 minLength: 6
 *                 example: "password123"
 *     responses:
 *       201:
 *         description: 用户创建成功
 *         content:
 *           application/json:
 *             example:
 *               message: "用户创建成功"
 *               data:
 *                 id: 3
 *                 username: "new_user"
 *                 email: "new@example.com"
 *       400:
 *         description: 参数错误（缺少字段/邮箱格式/已注册）
 *       500:
 *         description: 服务器错误
 */
router.post('/', userController.createUser);

// 更新用户
/**
 * @swagger
 * /users/{id}:
 *   put:
 *     summary: 更新用户信息
 *     tags: [Users]
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               username:
 *                 type: string
 *                 example: "updated_user"
 *               email:
 *                 type: string
 *                 format: email
 *                 example: "updated@example.com"
 *               password:
 *                 type: string
 *                 format: password
 *                 minLength: 6
 *                 example: "newpassword"
 *     responses:
 *       200:
 *         description: 更新成功
 *         content:
 *           application/json:
 *             example:
 *               message: "用户更新成功"
 *       404:
 *         description: 用户不存在
 *       400:
 *         description: 邮箱格式错误
 *       500:
 *         description: 服务器错误
 */
router.put('/:id', userController.updateUser);

// 删除用户
/**
 * @swagger
 * /users/{id}:
 *   delete:
 *     summary: 删除单个用户
 *     tags: [Users]
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: 删除成功
 *         content:
 *           application/json:
 *             example:
 *               message: "用户删除成功"
 *       404:
 *         description: 用户不存在
 *       500:
 *         description: 服务器错误
 */
router.delete('/:id', userController.deleteUser);

// 批量删除用户
/**
 * @swagger
 * /users/deleteBatch:
 *   post:
 *     summary: 批量删除用户
 *     tags: [Users]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - ids
 *             properties:
 *               ids:
 *                 type: array
 *                 items:
 *                   type: integer
 *                 example: [1, 2, 3]
 *     responses:
 *       200:
 *         description: 批量删除成功
 *         content:
 *           application/json:
 *             example:
 *               message: "批量删除成功"
 *               deletedCount: 3
 *       400:
 *         description: 无效的请求参数
 *       404:
 *         description: 未找到用户
 *       500:
 *         description: 服务器错误
 */
router.post('/deleteBatch', userController.deleteBatchUsers);

module.exports = router;